layui.use(['form','layer','jquery'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.$;

    //输入手机号获取验证码
    $("#getMsm").click(function (){
        var a = $("[name=userPhone]").val()
        var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
        if (a == "") {
            layer.msg('请输入手机号！！！',{time:1000,icon:5})
        } else if (!reg_tel.test(a)) {
            layer.msg('请填写正确的手机号！！！',{time:1000,icon:5})
        } else {
            $.ajax({
                type: 'post',
                url: '/getMSM',
                data: {'phone': a},
                dataType:'json',
                success:function (res){
                    if (res.state == '200'){
                        layer.msg("获取验证码成功",{time:500,icon:6})
                    }else {
                        layer.msg("获取验证码失败，手机格式有误",{time:1000,icon:5})
                    }

                },
                error:function (e){
                    console.log('请求失败')
                }
            })
        }

    })


    // 进行账号密码登录操作
    form.on('submit(login)', function (data) {
        data = data.field;
        console.log(data);

        $.ajax({
            type:'post',
            url: '/getLoginByAccount',
            data:data,
            success:function (s){
                if (s.state == 200){
                    //存入用户信息
                    sessionStorage.setItem("user",JSON.stringify(s.user))
                    layer.msg("登录成功",{time:500,icon:6},function (){
                        window.location ="/index/index.html";
                    })
                }else {
                    layer.msg("账户信息有误，登录失败",{time:700,icon:5},function (){
                        window.location.href="/login";
                    })
                }
            },
            error:function (e){
                console.log(e);
            }
        })
        return false;
    })


    // 进行手机号快速登录操作
    form.on('submit(ksLogin)', function (data) {
        data = data.field;
        console.log(data);

        $.ajax({
            type:'post',
            url: '/getLoginByPhone',
            data:data,
            success:function (res){
                if (res.state == 200){
                    sessionStorage.setItem("user",JSON.stringify(res.user))
                    layer.msg("登录成功",{time:500,icon:6},function (){
                        window.location ="index/index.html";
                    })
                }else if (res.state == 400){
                    layer.msg("没有用户信息，先进行注册",{time:1000,icon:5},function (){
                        window.location.href="/register";
                    })
                }else {
                    layer.msg("验证码错误，登录失败",{time:1000,icon:5},function (){
                        window.location.href="/login";
                    })
                }
            },
            error:function (e){
                console.log(e);
            }
        })
        return false;
    })
})
